package com.goodjia.phase1module3;

import java.util.Objects;

public class Worker {

    private String name;
    private int age;
    private double salary;

    // work方法打印当前调用对象名字正在工作
    public void work (){
        System.out.println(getName() + "正在工作");
    }

    public Worker() {
    }

    // 有参构造函数
    public Worker(String name, int age, double salary) {
        setName(name);
        setAge(age);
        setSalary(salary);
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public double getSalary() {
        return salary;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    /**
     * 重写equals函数
     * @param o 传入的要比较的对象
     * @return 返回值为姓名、薪水、年龄共同比较的结果 都相同时才返回true 否则返回false
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Worker worker = (Worker) o;
        return age == worker.age && Double.compare(worker.salary, salary) == 0 && Objects.equals(name, worker.name);
    }

    /**
     * 重写hashCode函数确保和equals一直
     * @return
     */
    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary);
    }

    /**
     * 重写toString函数打印worker对象时按return内容返回
     * @return
     */
    @Override
    public String toString() {
        return "Worker{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", salary=" + salary +
            '}';
    }

}
